﻿#include "weathercharts.h"
#include "ui_weathercharts.h"

weatherCharts::weatherCharts(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::weatherCharts)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QIcon icon(":/weather_return.png");
    ui->pushButton->setIcon(icon);
    ui->pushButton->setIconSize(ui->pushButton->size()); // 设置图标的尺寸

    sql=new mySql;
    // 启用滚动和缩放
    ui->widget->setRubberBand(QChartView::HorizontalRubberBand);
    ui->widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    ui->widget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    QLineSeries *series_1=new QLineSeries;
    QLineSeries *series_2=new QLineSeries;
    series_1->setName("最低温度");
    series_2->setName("最高温度");
    QVector<weather> vect;
    sql->getAllWeather(vect);
    // 添加数据点
    int yutian=0;
    int suntian=0;
    int duoyun=0;
    int yingtian=0;
    int good=0;
    int liang=0;
    int qingdu=0;
    int zhongdu=0;
    int mostdu=0;
    for(int i=0; i<vect.size(); i++)
    {
        series_1->append(i-1, vect[i].low);
        series_2->append(i-1, vect[i].high);
        if(vect[i].type=="晴")
        {
            suntian++;
        }
        else if(vect[i].type=="阴")
        {
            yingtian++;
        }
        else if(vect[i].type=="多云")
        {
            duoyun++;
        }
        else
        {
            yutian++;
        }
        if(vect[i].aqi<=50)
        {
            good++;
        }
        else if(vect[i].aqi<=100)
        {
            liang++;
        }
        else if(vect[i].aqi<=150)
        {
            qingdu++;
        }
        else if(vect[i].aqi<=200)
        {
            zhongdu++;
        }
        else
        {
            mostdu++;
        }
    }

    // 创建散点图系列来标记数据点
    QScatterSeries *scatter_1 = new QScatterSeries();
    QScatterSeries *scatter_2 = new QScatterSeries();

    // 设置散点图的样式
    scatter_1->setMarkerSize(6); // 设置标记大小
    scatter_1->setColor(Qt::blue); // 设置标记颜色
    scatter_2->setMarkerSize(6);
    scatter_2->setColor(Qt::green);

    // 将数据点添加到散点图系列
    for(int i=0; i<vect.size(); i++)
    {
        scatter_1->append(i-1, vect[i].low);
        scatter_2->append(i-1, vect[i].high);
    }

    // 创建图表
    QChart *chart = new QChart();
    chart->setTitle("未来15天温度预测趋势图");
    chart->addSeries(series_1);
    chart->addSeries(series_2);
    chart->addSeries(scatter_1);
    chart->addSeries(scatter_2);

    // 创建X轴和Y轴
    QValueAxis *axisX = new QValueAxis();
    QValueAxis *axisY = new QValueAxis();

    // 设置X轴范围
    axisX->setRange(-1, 14);
    axisX->setTickCount(16); // 包括起始点和结束点
    axisX->setLabelFormat("%d");
    // 设置Y轴范围和格式
    axisY->setRange(0, 50);
    axisY->setTickCount(6);
    axisY->setLabelFormat("%d");

    // 将轴添加到图表
    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);

    // 将系列附加到轴
    series_1->attachAxis(axisX);
    series_1->attachAxis(axisY);
    series_2->attachAxis(axisX);
    series_2->attachAxis(axisY);
    scatter_1->attachAxis(axisX);
    scatter_1->attachAxis(axisY);
    scatter_2->attachAxis(axisX);
    scatter_2->attachAxis(axisY);

    chart->setAnimationOptions(QChart::AllAnimations);
    ui->widget->setChart(chart);
    ui->widget->setRenderHint(QPainter::Antialiasing);
    QPieSeries *series_3=new QPieSeries();
    QPieSlice *slice_1=series_3->append("晴",suntian);
    QPieSlice *slice_2=series_3->append("云",duoyun);
    QPieSlice *slice_3=series_3->append("阴",yingtian);
    QPieSlice *slice_4=series_3->append("雨",yutian);
    slice_1->setColor(QColor(255, 255, 0)); // 晴：黄色
    slice_2->setColor(QColor(192, 192, 192)); // 云：灰色
    slice_3->setColor(QColor(128, 128, 128)); // 阴：深灰色
    slice_4->setColor(QColor(0, 0, 255)); // 雨：蓝色

    chart=new QChart();
    chart->addSeries(series_3);
    chart->setTitle("未来15天天气状况");
    chart->setAnimationOptions(QChart::AllAnimations);
    ui->widget_2->setChart(chart);
    ui->widget_2->setRenderHint(QPainter::Antialiasing);

    QPieSeries *series_4=new QPieSeries();
    QPieSlice *slice_5=series_4->append("优",good);
    QPieSlice *slice_6=series_4->append("良",liang);
    QPieSlice *slice_7=series_4->append("轻",qingdu);
    QPieSlice *slice_8= series_4->append("中",zhongdu);
    QPieSlice *slice_9= series_4->append("重",mostdu);
    slice_5->setColor(QColor(0, 255, 0));  // 优：绿色 (RGB: 0, 255, 0)
    slice_6->setColor(QColor(255, 255, 0)); // 良：黄色 (RGB: 255, 255, 0)
    slice_7->setColor(QColor(255, 165, 0)); // 轻：橙色 (RGB: 255, 165, 0)
    slice_8->setColor(QColor(255, 0, 0));  // 中：红色 (RGB: 255, 0, 0)
    slice_9->setColor(QColor(128, 0, 0));  // 重：深红色 (RGB: 128, 0, 0)
    chart=new QChart();
    chart->addSeries(series_4);
    chart->setTitle("未来15天空气质量");
    chart->setAnimationOptions(QChart::AllAnimations);
    ui->widget_3->setChart(chart);
    ui->widget_3->setRenderHint(QPainter::Antialiasing);
}

void weatherCharts::backWidget(QWidget *parent)
{
    back=parent;
}

weatherCharts::~weatherCharts()
{
    delete ui;
}

void weatherCharts::on_pushButton_clicked()
{
    back->show();
    this->hide();
}
